﻿using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;

namespace AXExtension.Filters
{
	/// <summary>
	/// Ensures that grid cells are found by using
	/// their name, rather than their value, to make
	/// grid testing far more reliable
	/// </summary>
	class GridFilter : Filter
	{
		public GridFilter()
		{
			Require(stack => stack.Peek().UIElement.ControlTypeName == "Cell");
		}

		public override void Run(IUITestActionStack stack)
		{
			UITestAction action = stack.Peek();
			action.UIElement.QueryId.Condition =
						new AndCondition(new PropertyCondition("Name", action.UIElement.Name));

			// The parent row must also be referred to by name.
			var parent = action.UIElement.QueryId.Ancestor;
			if (parent != null)
			{
				parent.QueryId.Condition = new AndCondition(new PropertyCondition("Name", parent.Name));
			}
		}
	}
}
